// pages/subscribe/subscribe.js
let app = getApp()
let request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight,
    scrollHeight: app.globalData.pageAllHeight,
    operationStatus: 0,
    setStatusDialog: false,
    pageNum: 1,
    pageSize: 20,
    pageFlag: false,
    subscribeList: [],
    statusList: ['待入场', '已完成', '未入场', '已驳回', '已取消'],
    statusIndex: 0,
    reserveList: [],
    reserveData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSubscribeList()
    this.setData({
      operationStatus: wx.getStorageSync('lmtc_admin_parkInfo').reserveStatus
    })
  },
  onReady: function () {
    let that = this
    let height
    wx.createSelectorQuery().select('.subscribe-set').boundingClientRect(rect => {
      height = rect.height;
      wx.createSelectorQuery().select('.subscribe-set-title').boundingClientRect(rect => {
        height += rect.height;
        wx.createSelectorQuery().select('.subscribe-status-list').boundingClientRect(rect => {
          height += rect.height;
          that.setData({
            scrollHeight: app.globalData.pageAllHeight - height * 2 - 100
          })
        }).exec();
      }).exec();
    }).exec();
  },
  /**
   * 操作预约功能
   */
  operationSetStatus(e) {
    let that = this
    let status = e.currentTarget.dataset.status
    wx.showModal({
      title: '提示',
      content: '是否' + (status == 0 ? '开启' : '关闭') + '预约功能',
      success(res) {
        if (res.confirm) {
          that.setData({
            operationStatus: status == 0 ? 1 : 0
          })
          let parmas = {
            parkId: wx.getStorageSync('lmtc_admin_parkInfo').parkId,
            status: that.data.operationStatus
          }
          request.http('/promotion/reserve-rule/updateReserveStatus', 'POST', parmas)
            .then(res => {
              let data = res.data
              if (data.code == 200) {
                let parkInfo = wx.getStorageSync('lmtc_admin_parkInfo')
                parkInfo.reserveStatus = that.data.operationStatus
                wx.setStorageSync('lmtc_admin_parkInfo', parkInfo)
                wx.showToast({
                  title: '您已' + (status == 0 ? '开启' : '关闭') + '预约功能',
                })
              }
            })
        }
      }
    })
  },
  /**
   * 打开预约设置弹窗
   */
  showSetStatusDialog() {
    let that = this
    let parmas = {
      parkId: wx.getStorageSync('lmtc_admin_parkInfo').parkId
    }
    request.http('/promotion/reserve-rule/getReserveRuleByParkId', 'GET', parmas)
      .then(res => {
        let data = res.data
        let reserveData = {}
        if (data.code == 200) {
          reserveData = {
            amountUp: data.data[0].amount,
            amountDown: data.data[1].amount,
            reserveParkSpace: data.data[0].reserveParkSpace
          }
          that.setData({
            reserveList: data.data,
            reserveData,
            setStatusDialog: true
          })
        }
      })
  },
  /**
   * 减车位数
   */
  subNumber() {
    let reserveParkSpace = this.data.reserveData.reserveParkSpace
    if (reserveParkSpace <= 0) {
      wx.showToast({
        title: '最少不低于0个',
        icon: 'none'
      })
      return
    }
    reserveParkSpace--
    this.setData({
      'reserveData.reserveParkSpace': reserveParkSpace
    })
  },
  /**
   * 增加车位数
   */
  addNumber() {
    let reserveParkSpace = this.data.reserveData.reserveParkSpace
    reserveParkSpace++
    this.setData({
      'reserveData.reserveParkSpace': reserveParkSpace
    })
  },
  /**
   * 设置车位数
   */
  setNumber(e) {
    let val = e.detail.value
    this.setData({
      'reserveData.reserveParkSpace': val
    })
  },
  /**
   * 减上午服务费
   */
  subAmountUp() {
    let amountUp = this.data.reserveData.amountUp
    if (amountUp <= 0) {
      wx.showToast({
        title: '最少不低于0个',
        icon: 'none'
      })
      return
    }
    amountUp--
    this.setData({
      'reserveData.amountUp': amountUp
    })
  },
  /**
   * 增加上午服务费
   */
  addAmountUp() {
    let amountUp = this.data.reserveData.amountUp
    amountUp++
    this.setData({
      'reserveData.amountUp': amountUp
    })
  },
  /**
   * 设置上午服务费
   */
  setAmountUp(e) {
    let val = e.detail.value
    this.setData({
      'reserveData.amountUp': val
    })
  },
  /**
   * 减上午服务费
   */
  subAmountDown() {
    let amountDown = this.data.reserveData.amountDown
    if (amountDown <= 0) {
      wx.showToast({
        title: '最少不低于0个',
        icon: 'none'
      })
      return
    }
    amountDown--
    this.setData({
      'reserveData.amountDown': amountDown
    })
  },
  /**
   * 增加上午服务费
   */
  addAmountDown() {
    let amountDown = this.data.reserveData.amountDown
    amountDown++
    this.setData({
      'reserveData.amountDown': amountDown
    })
  },
  /**
   * 设置上午服务费
   */
  setAmountDown(e) {
    let val = e.detail.value
    this.setData({
      'reserveData.amountDown': val
    })
  },
  /**
   * 关闭预约设置弹窗
   */
  closeSetStatusDialog() {
    this.setData({
      setStatusDialog: false
    })
  },
  /**
   * 设置预约功能
   */
  setReserve() {
    let that = this
    let parmas = {
      amountDown: this.data.reserveData.amountDown,
      amountUp: this.data.reserveData.amountUp,
      reserveParkSpace: this.data.reserveData.reserveParkSpace,
      reserveFeeScaleIdDown: this.data.reserveList[1].id,
      reserveFeeScaleIdUp: this.data.reserveList[0].id
    }
    request.http('/promotion/reserve-rule/updateReserveFeeScale', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({
            title: '设置成功',
          })
          that.setData({
            setStatusDialog: false
          })
        }
      })
  },
  getSubscribeType(e) {
    let index = e.currentTarget.dataset.index
    if (index == this.data.statusIndex) { return }
    this.setData({
      statusIndex: index,
      pageNum: 1
    })
    this.getSubscribeList(false, index + 1)
  },
  /**
   * 获取预约列表
   */
  getSubscribeList(next, status) {
    status = status ? status : 1
    let that = this
    let parmas = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      status: status,
      parkId: wx.getStorageSync('lmtc_admin_parkInfo').parkId
    }
    request.http('/order-reserve/park-reserve-record/get-reserve-record', 'GET', parmas)
      .then(res => {
        let data = res.data
        let subscribeList = that.data.subscribeList
        let pageFlag = that.data.pageFlag
        if (data.code == 200) {
          if (next) {
            if (data.data.records.length > 0) {
              subscribeList = subscribeList.concat(data.data.records)
              pageFlag = false
            } else {
              pageFlag = true
            }
          } else {
            subscribeList = data.data.records
          }
          that.setData({
            subscribeList,
            pageFlag
          })
        }
      })
  },
  /**
   * 触底加载
   */
  scrollTolower() {
    if (this.data.pageFlag) {
      return
    }
    this.setData({
      pageFlag: true,
      pageNum: this.data.pageNum + 1
    })
    this.getSubscribeList(true)
  },
  /**
   * 跳转页面
   * @param {*} e 
   */
  toPage(e) {
    let status = e.currentTarget.dataset.status
    let url
    if (status == 1) {
      url = '/pages/apply/apply'
    } else if (status == 2) {
      url = '/pages/backlog/backlog'
    }
    if (!url) {
      return
    }
    wx.navigateTo({
      url
    })
  }
})